<%@page import="org.apache.tomcat.jni.Directory"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="org.apache.tomcat.jni.Time"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="base.Base"%>
<%@page import="sosr.bean.Patient"%>
<%@page import="bean.Valider"%>
<jsp:useBean id="patient" scope="request" class="sosr.bean.Patient"></jsp:useBean>
<jsp:useBean id="pageConfig" scope="session" class="bean.Page"></jsp:useBean>
<jsp:useBean id="pageCGI" scope="session" class="bean.Page"></jsp:useBean>
<% 
int id = pageConfig.getAction_id(); 
%>

<%
String civilite = "";
String nom = "";
String prenom = "";
String date_naissance = null;
int poids = 0;
String email = "";
String commentaires = "";


Base db = new Base();

request.setCharacterEncoding("UTF-8");
if (request.getParameter("submit") != null) {
	boolean res = Valider.form(request, patient);
	id = Integer.parseInt(request.getParameter("id"));
	civilite = request.getParameter("civilite");
	nom = request.getParameter("nom");
	prenom = request.getParameter("prenom");
	date_naissance = request.getParameter("date_naissance");
	poids = Integer.parseInt(request.getParameter("poids"));
	email = request.getParameter("email");
	commentaires = request.getParameter("commentaire");
	
	
	
	if (res) {
		Patient p = new Patient(id,civilite,nom,prenom,date_naissance,poids,email,commentaires);
		if (id != -1) {
			db.ouvrir();
			boolean rp = db.modPatient(id,p);
			db.fermer();
			if (rp) {
				pageCGI.setMessage("Notification","Le patient "+civilite+" "+prenom+" "+nom+" &agrave; &eacute;t&eacute; modifi&eacute; avec succ&egrave;s !");
				response.sendRedirect("../../patients.jsp");
			}
		}
		else {
			db.ouvrir();
			boolean rp = db.addPatient(p);
			db.fermer();
			if (rp) {
				pageCGI.setMessage("Notification","Le patient "+civilite+" "+prenom+" "+nom+" &agrave; &eacute;t&eacute; ajout&eacute; avec succ&egrave;s !");
				response.sendRedirect("../../patients.jsp");
			}
		}
	}
}
else {
	
	Patient p = null;
	System.out.println("## id = "+id);
	if (id != -1) {
		db.ouvrir();
		p = db.selectPatient(id);
		db.fermer();
		if (p!=null) {
			civilite = p.getCivilite();
			nom = p.getNom();
			prenom = p.getPrenom();
			date_naissance = p.getDate_naissance();
			poids = p.getPoids();
			email = p.getEmail();
			commentaires = p.getCommentaire();
		}
	}



%>

<% if (id != -1) { %>
<h1>Editer la fiche patient #<%=id %></h1>
<% } else { %>
<h1>Ajouter un nouveau patient</h1>
<% } %>
<form action="pages/patient/form_newpatient.jsp" method="post" id="formpatient" >
<input type="hidden" name="id" value="<%=id %>" />
<table cellpadding="0" cellspacing="0" class="main_form">
	<tr>
		<td>Civilit&eacute; *</td>
		<td>
			<select name="civilite" id="civilite" >
				<option value="M" <% if(civilite.equals("M")) out.print("selected='selected' "); %> >M.</option>
				<option value="Mme" <% if(civilite.equals("Mme")) out.print("selected='selected' "); %> >Mme.</option>
				<option value="Mle" <% if(civilite.equals("Mle")) out.print("selected='selected' "); %> >Mle.</option>
			</select>
		</td>
	</tr>
	<tr>
		<td>Nom *</td>
		<td><input type="text" name="nom" id="nom" value="<%=nom %>" class="field" /></td>
	</tr>
	<tr>
		<td>Pr&eacute;nom *</td>
		<td><input type="text" name="prenom" id="prenom" value="<%=prenom %>" class="field" /></td>
	</tr>
	<tr>
		<td>Date de naissance *&nbsp;</td>
		<td><input type="text" name="date_naissance" id="date_naissance" value="<% if (date_naissance != null) out.print(date_naissance); %>" class="field date_field" /></td>
	</tr>
	<tr>
		<td>Poids (en g) *</td>
		<td><input type="text" name="poids" id="poids" value="<% if(poids != 0) out.print(poids); %>" class="field" /></td>
	</tr>
	<tr>
		<td>Email *</td>
		<td><input type="text" name="email" id="email" value="<%=email %>" class="field" /></td>
	</tr>
	<tr>
		<td>Commentaires </td>
		<td><textarea name="commentaire" id="commentaire" ><%=commentaires %></textarea></td>
	</tr>
	
	<tr><td>&nbsp;</td><td></td></tr>
	<tr><td colspan="2"><em>* champs obligatoires</em></td></tr>
	<tr><td>&nbsp;</td><td></td></tr>
	<tr>
		<td colspan="2">
			<input type="submit" class="bouton" name="submit" value="Enregistrer" style="float: right" />
			<input type="reset" class="bouton" value="R&eacute;initialiser" style="float: right" />
		</td>
	</tr>
</table>
</form>

<% } %>
<script type="text/javascript">
$("#formpatient").submit(function() {
    if (check()) {
      return true;
    }
    alert2("Veuillez renseigner l'ensemble des champs");
    return false;
  });

function check() {
	if ($("#civilite").val() == ""
		|| $("#nom").val() == ""
		|| $("#prenom").val() == ""
		|| $("#date_naissance").val() == ""
		|| $("#poids").val() == ""
		|| $("#email").val() == "") {
		return false;
	}
	else {
		return true;
	}
}
$(function() {
	$( "#date_naissance" ).datepicker({
		showOn: "both",
		buttonImage: "images/ihm/ic_cal_20.png",
		buttonImageOnly: true
	});
});
</script>